﻿using System;
using System.Collections.Generic;
using Framework.Business;
using Framework.Helper;
using MongoDB.Driver;
using RealDataReceiver.Entities;

namespace RealDataReceiver.DataAccess
{
    public interface IRealDataDA
    {
        void SaveMany(List<DeviceMetaData> metaDataList);
        void SaveOne(DeviceMetaData metaData);

        IMongoCollection<DeviceMetaData> GetCollection();

    }
    public class RealDataDA: IRealDataDA
    {
        private readonly IMongoCollection<DeviceMetaData> _mongoCollectionHistory;
        private RealDataDA()
        {
            var client = new MongoClient(ConfigurationHelper.GetConfiguration("ConnectionString"));
            var database = client.GetDatabase(ConfigurationHelper.GetConfiguration("DBInstance"));
            _mongoCollectionHistory = database.GetCollection<DeviceMetaData>(ConfigurationHelper.GetConfiguration("Collection"));
            
        }

        static RealDataDA()
        {
            InstanceManager.RegisterBuilder<IRealDataDA,RealDataDA>(() => new RealDataDA());
        }

        public static IRealDataDA Instance
        {
            get { return InstanceManager.GetInstance<IRealDataDA>(); }
        }

        public IMongoCollection<DeviceMetaData> Collection
        {
            get { return _mongoCollectionHistory; }
        }

        public IMongoCollection<DeviceMetaData> GetCollection()
        {
            return _mongoCollectionHistory;
        }
        public void SaveOne(DeviceMetaData metaData)
        {
            try
            {
                _mongoCollectionHistory.InsertOneAsync(metaData);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public void SaveMany(List<DeviceMetaData> metaDataList)
        {
            try
            {
                _mongoCollectionHistory.InsertManyAsync(metaDataList);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}
